package com.jzo2o.customer.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.api.publics.SmsCodeApi;
import com.jzo2o.common.constants.UserType;
import com.jzo2o.common.enums.SmsBussinessTypeEnum;
import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.customer.mapper.ServeProviderMapper;
import com.jzo2o.customer.model.domain.ServeProvider;
import com.jzo2o.customer.model.dto.request.InstitutionRegisterReqDTO;
import com.jzo2o.customer.service.IRegisterService;
import com.jzo2o.customer.service.IServeProviderService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author wanfeng
 * @version 1.0
 * @since 2025-09-16 15:16
 */
@Service
public class RegisterServiceImpl extends ServiceImpl<ServeProviderMapper, ServeProvider> implements IRegisterService {
    @Resource
    private SmsCodeApi smsCodeApi;
    @Resource
    private IServeProviderService serveProviderService;
    @Resource
    private PasswordEncoder passwordEncoder;
    /**
     * 机构人员注册
     * @param registerReqDTO
     */
    @Override
    @Transactional
    public void institutionRegister(InstitutionRegisterReqDTO registerReqDTO) {
        //验证码不能为空
        if(registerReqDTO.getVerifyCode() == null){
            throw new ForbiddenOperationException("验证码不能为空");
        }
        //远程调用验证验证码是否正确
        boolean verifyResult = smsCodeApi.verify(registerReqDTO.getPhone(), SmsBussinessTypeEnum.INSTITION_REGISTER, registerReqDTO.getVerifyCode()).getIsSuccess();
        if(!verifyResult) {
            throw new BadRequestException("验证码错误，请重新获取");
        }
        //加密
        String password = passwordEncoder.encode(registerReqDTO.getPassword());
        //注册
        serveProviderService.add(registerReqDTO.getPhone(), UserType.INSTITUTION, password);
    }
}
